var app = getApp();
Page({
  data: {
  	orderlist:null,
  	dealer:null,
  	userInfo:null,
  	over:0,
  	type:0,
  	opens:0,
  	userCouponList:[], //优惠券列表
  	couponindex:-1, //可用优惠券索引
  	couponpice:0,//优惠金额
  	couponafter:0,//优惠后
  	fromhide:0,//是否要显示提交手机号
  	realName:'',//姓名
  	mobile:'',//手机号
  	subst:true
  },
  onLoad(options) {
  	var that=this;
  	this.data.type=options.type
  	this.setData({
  		dealer:wx.getStorageSync('dealer'),
  		orderlist:wx.getStorageSync('orderlist'),
  		userInfo:wx.getStorageSync('userInfo')
    });
    
    console.log(that.data.userInfo.mobile)
    if(that.data.userInfo.mobile==null){
    	this.setData({
	  		fromhide:1
	    });
    }
    
    //加载优惠券数据
    app.globalAjax(
	  		'/api/coupon/get',
	  		"get",
	  		{},
	  		function(e){
	  			if(e.code==0)
	      	{
	      		//判断优惠券是否可用
	      		var couponindex=-1;
	      		for(var i in e.userCouponList){
	      			var $t=e.userCouponList[i],
	      					full=$t.coupon.moneyFull;
	      				if(Number(that.data.orderlist.total) >= Number(full)){
	      					if(couponindex==-1){
	      						couponindex=i;//赋值默然优惠券
	      					}
	      					$t.types=1;//可用
	      				}else{
	      					$t.types=0;//不可用
	      				}
	      		};
	      		//end 
	      		that.setData({
	      				couponindex:couponindex,
	      				userCouponList:e.userCouponList
					  });
					  that.setpice()//设置金额
					  
					  console.log(that.data.couponindex)
					  console.log(e.userCouponList);
					  
	      	}else{
	      		wx.showToast({
				        title: e.msg,
				        icon: "none"
				      });
	      	}
	  	})
    //end
    
  },
  subname:function(e){//第一次提交手机号姓名
  	var that=this;
  	var post=e.detail.value
  	console.log(post)
  	if(post.realName==""){
  		wx.showToast({
        title: "用户名不能为空",
        icon: "none"});
  		return false;
  	}
  	if(post.mobile==""){
  		wx.showToast({
        title: "手机号不能为空",
        icon: "none"});
  		return false;
  	}
		
		 //是否为手机号码
    if (!/(^1[1|2|3|4|5|6|7|8|9][0-9]{9}$)/.test(post.mobile)) {
        wx.showToast({
	        title: "手机号格式不正确",
	        icon: "none"});
	  		return false;
    }
		
		
  	var info=wx.getStorageSync('userInfo');
				info.realName=post.realName
				info.mobile=post.mobile
		//post.gender=info.gender
  	app.globalAjax(
	  		'/api/member/update',
	  		"post",
	  		post,
	  		function(e){
	  			if(e.code==0)
	      	{
	      		wx.setStorage({
						   key: "userInfo",
						   data: info //需要存储的数据
						})
						that.setData({
					  		fromhide:0
					  });
	      		wx.showToast({
						    title: '提交成功',
						    icon: 'none',
						    duration: 1000,
						    success: function () {
						    }
						});
						
						
	      	}else{
	      		wx.showToast({
				        title: e.msg,
				        icon: "none"
				      });
	      	}
	  	})
  	
  	
  },
  setopens:function(){//弹出优惠
  	var $th=this;
  	if($th.data.couponindex==-1) return false;
  	var o=$th.data.opens?0:1;
		$th.setData({
  		opens:o
    });
  },
  selectcoupon:function(e){//选择优惠
  	var $th=this,
  			cur=e.currentTarget.dataset;
  			if(cur.types==0) return false;
				$th.setData({
		  		couponindex:cur.index
		    });
		    $th.setopens()
		    $th.setpice()//设置金额
  },
  setpice:function(e){//设置金额
  	var $th=this,
  			opirce=$th.data.orderlist.total,//原始总价
  			cpirce=0,//优惠金额
  			apirce=0//优惠后金额
  		
  		if($th.data.couponindex!=-1){//有优惠券可用
  			cpirce=$th.data.userCouponList[$th.data.couponindex].coupon.moneyReduce;
  		}
	  	apirce=Number(opirce)-Number(cpirce);
	  	$th.setData({
	  		couponpice:parseFloat(cpirce).toFixed(2),//优惠金额
	  		couponafter:parseFloat(apirce).toFixed(2)//优惠后
	    });
  },
  sub:function(e){//提交订单 并付款
  	
  	var $th=this;
  	var that=this.data;
  	var formId = e.detail.formId;
  	if(!that.subst)  return false;
  	that.subst=false;
  	
  	var couponId='';
  	if($th.data.couponindex!=-1){//有优惠券可用
  			couponId=$th.data.userCouponList[$th.data.couponindex].id;
  	}
  	var datas={
  		orderGoodsList:that.orderlist.orderGoodsList,
			remark: "",
			storeId: that.dealer.id,
			totalAmount: that.couponafter,
			couponId:couponId,
	  	formId:formId
		};
  	app.globalAjax(
	  		'/api/order/create',
	  		"post",
	  		datas,
	  		function(e){
	  			if(e.code==0)
	      	{
	      		//如果是购物车下单 移除购物车
	      		if($th.data.type=="cart"){
	      			var cart=wx.getStorageSync('cart');
	      			for(var i in datas.orderGoodsList){
	      				console.log(datas.orderGoodsList[i])
	      				var t=datas.orderGoodsList[i]
	      				var sets=cart[t.categoryId+"_"+t.goodsId+"_"+t.specsId]
								  	if(sets){
//								  		console.log("删除")
								  			delete cart[t.categoryId+"_"+t.goodsId+"_"+t.specsId];
								  	}
	      			}
	      			wx.setStorage({
							   key: "cart",
							   data: cart //需要存储的数据
							})
	      		}
            //end
            
            wx.redirectTo({
              url:'/pages/order/order/order?type=0'
            })
            return;
	      		
			      //跳转付款
			      app.globalAjax(
					  		'/api/wechat/pay/unifiedOrder',
					  		"post",
					  		{
					  			orderNumber:e.orderNumber,
					  			totalAmount:0.1
					  		},
							  		function(e2){
							  			if(e2.code==0)
							      	{
							      		app.pays(e2.data,function(ee){
							      			$th.setData({
											  		over:1
											    });
							      		},function(ee){
							      			setTimeout(function(){
							      				wx.redirectTo({
								      				url:'/pages/order/order/order?type=0'
								      			})
							      			},500)
							      		})
							      	}else{
							      		wx.showToast({
										        title: e2.msg,
										        icon: "none"
										      });
							      	}
							  },{token:wx.getStorageSync("token")})
				  
	      	}else{
	      		wx.showToast({
				        title: e.msg,
				        icon: "none"
				      });
	      	}
	  	},{token:wx.getStorageSync("token")})
  	
  }
})